{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ogr, gdal\n",
    "import os\n",
    "import pandas as pd\n",
    "path = os.getcwd()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "xzqhshp = path +\"/china/\"+ \"cn.shp\"\n",
    "outfile = path + \"/china_CET.shp\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "inDriver = ogr.GetDriverByName(\"ESRI Shapefile\")\n",
    "inDataSource = inDriver.Open(xzqhshp, 0)\n",
    "inLayer = inDataSource.GetLayer()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "outDriver = ogr.GetDriverByName(\"ESRI Shapefile\")\n",
    "gdal.SetConfigOption(\"SHAPE_ENCODING\", \"CP936\")\n",
    "outDataSource = outDriver.CreateDataSource(outfile)\n",
    "outLayer = outDataSource.CreateLayer(\"china_centroids\", \n",
    "                                     geom_type=ogr.wkbPoint)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 将原图层中所有的字段，都复制到新图层中去"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "inLayerDefn = inLayer.GetLayerDefn()\n",
    "for i in range(0, inLayerDefn.GetFieldCount()):\n",
    "    fieldDefn = inLayerDefn.GetFieldDefn(i)\n",
    "    outLayer.CreateField(fieldDefn)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 创建输出图层"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "outLayerDefn = outLayer.GetLayerDefn()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 将原来图层中的所有属性写入到中心点图层中去"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "POINT (116.385098507776 40.1506594296298)\n",
      "POINT (117.361118508898 39.296182780641)\n",
      "POINT (112.295048888067 37.5633333264362)\n",
      "POINT (122.583403038968 41.3017833678349)\n",
      "POINT (126.229192017278 43.6611464914497)\n",
      "POINT (127.762120465654 47.8588088256495)\n",
      "POINT (121.435915853131 31.2499834245788)\n",
      "POINT (119.513242540706 32.905490178672)\n",
      "POINT (120.141545190149 29.1515718967945)\n",
      "POINT (117.23789890862 31.776248151666)\n",
      "POINT (117.985332429825 26.0917207078702)\n",
      "POINT (115.69606885646 27.5804331085672)\n",
      "POINT (118.154802846355 36.3347979853928)\n",
      "POINT (113.650319872937 33.8822621177884)\n",
      "POINT (112.293488431052 30.9675739537212)\n",
      "POINT (111.706756356294 27.6550029124676)\n",
      "POINT (113.441735734061 23.332137943794)\n",
      "POINT (108.818817156297 23.8616928914029)\n",
      "POINT (109.749478298509 19.2311693621364)\n",
      "POINT (107.890617389692 30.0927178950061)\n",
      "POINT (102.667488697926 30.6219837503356)\n",
      "POINT (106.846618662682 26.829143097744)\n",
      "POINT (101.478400495478 24.9680125532225)\n",
      "POINT (88.4453255108923 31.486284234739)\n",
      "POINT (108.894640663619 35.1803335850772)\n",
      "POINT (96.0344595351111 35.6685728030387)\n",
      "POINT (106.210481257318 37.2740003634849)\n",
      "POINT (85.1979108854025 41.1167530245297)\n",
      "POINT (120.965961069659 23.7279917624602)\n",
      "POINT (113.903682750331 44.0821641598952)\n",
      "POINT (100.688292772024 37.832408430075)\n",
      "POINT (116.152541941565 39.5738593456418)\n"
     ]
    }
   ],
   "source": [
    "for i in range(inLayer.GetFeatureCount()):\n",
    "    inFeature = inLayer.GetFeature(i)\n",
    "    outFeature = ogr.Feature(outLayerDefn)\n",
    "    for j in range(outLayerDefn.GetFieldCount()):\n",
    "        outFeature.SetField(outLayerDefn.GetFieldDefn(j).GetNameRef(),\n",
    "                            inFeature.GetField(j))\n",
    "    geom = inFeature.GetGeometryRef()\n",
    "    centroid = geom.Centroid()\n",
    "    print(centroid)\n",
    "    outFeature.SetGeometry(centroid)\n",
    "    outLayer.CreateFeature(outFeature)\n",
    "    outFeature = None"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 设置为空，表示数据push到文件中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "inDataSource = None\n",
    "outDataSource = None"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
